package com.lv.common.widget.behavior;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.ViewCompat;

import com.google.android.material.appbar.AppBarLayout;

/**
 * 作者： ygx
 * 创建日期：2025/3/1
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：TODO
 */
public class InstantFloatingBehavior extends AppBarLayout.Behavior {

    private float lastY = 0f;
    private boolean isScrollingUp = false;

    public InstantFloatingBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastY = ev.getY();
                isScrollingUp = false;
                break;
            case MotionEvent.ACTION_MOVE:
                float currentY = ev.getY();
                if (currentY < lastY) {
                    // 用户正在向上滑动
                    isScrollingUp = true;
                } else {
                    isScrollingUp = false;
                }
                lastY = currentY;
                break;
        }
        // 始终允许拦截，以便我们可以实时响应滑动
        return true;
    }

    @Override
    public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout,
                                  @NonNull AppBarLayout child,
                                  @NonNull View target,
                                  int dx,
                                  int dy,
                                  @NonNull int[] consumed,
                                  int type) {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);

        if (isScrollingUp && dy > 0) {
            // 用户正在向上滑动，立即折叠 AppBarLayout
            if (child.getTop() < 0) { // 根据需要调整条件
                child.setExpanded(false, true);
            }
        } else if (!isScrollingUp && dy < 0) {
            // 用户向下滑动，展开 AppBarLayout（可选）
            child.setExpanded(true, true);
        }
    }
    @Override
    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout,
                                       @NonNull AppBarLayout child,
                                       @NonNull View directTargetChild,
                                       @NonNull View target,
                                       int axes, int type) {
        return axes == ViewCompat.SCROLL_AXIS_VERTICAL;
    }
}
